module main

struct ExpectedIdentifier {
	identifier string
}

fn test_parser() {
	input := 'let x = 5; let y = 10; let foobar = 8888;'

	mut l := Lexer.new(input)
	mut p := Parser.new(l)
	program := p.parse_program()

	if isnil(program) {
		println('parse_program() return nil')
	}

	if program.statements.len != 3 {
		println('program.statements does not contain 3 statements. got=${program.statements.len}')
	}

	tests := [
		ExpectedIdentifier{
			identifier: 'x'
		},
		ExpectedIdentifier{
			identifier: 'y'
		},
		ExpectedIdentifier{
			identifier: 'foobar'
		},
	]

	for idx, tt in tests {
		stmt := program.statements[idx]
		if !is_let_statement(stmt, tt.identifier) {
			return
		}
	}
}

fn is_let_statement(stmt Statement, name string) bool {
	if stmt.token_literal() != 'let' {
		println("let_stmt.token_literal not 'let'. got=${stmt.token_literal()}")
		return false
	}

	if stmt !is LetStatement {
		println('stmt not LetStatement, got=${stmt}')
		return false
	}

	let_stmt := stmt as LetStatement
	if let_stmt.name.value != name {
		println("let_stmt.name.value not '${name}', got=${let_stmt.name.value}")
		return false
	}

	if let_stmt.name.token_literal() != name {
		println("let_stmt.name not '${name}', got=${let_stmt.name}")
		return false
	}

	return true
}
